#include "../pch.h"

namespace BFX
{

REFUniTextReader::REFUniTextReader()
{
}


REFUniTextReader::~REFUniTextReader()
{
}

REFUniStringReader::REFUniStringReader(const UniString& str)
{
	m_str = str;
	m_nPos = 0;
}

REFUniStringReader::~REFUniStringReader()
{
}

// Reads a block of characters.
// This method will read up to count characters from this textreader into the buffer character array starting at position index.
// Returns the actual number of characters read.
int REFUniTextReader::Read(OUT UCHAR* pBuffer, int iOffset, int nLength)
{
	BFX_REQUIRE (pBuffer != NULL);
	BFX_REQUIRE (iOffset >= 0 && nLength >= 0);

	int n = 0;
	do
	{
		int ch = Read();
		if (ch == -1) break;
		pBuffer[iOffset + n++] = (UCHAR)ch;
	}
	while (n < nLength);
	return n;
}

// Returns the next available character without actually reading it from the input stream.
// The current position of the TextReader is not changed by this operation.
// The returned value is -1 if no further characters are available.
int REFUniStringReader::Peek()
{
	if (m_nPos == m_str.GetLength())
		return -1;
	return m_str[m_nPos];
}

// Reads the next character from the input stream
// The returned value is -1 if no further characters are available.
int REFUniStringReader::Read()
{
	if (m_nPos == m_str.GetLength())
		return -1;
	return m_str[m_nPos++];
}

// Reads a block of characters.
// This method will read up to count characters from this textreader into the buffer character array starting at position index.
// Returns the actual number of characters read.
int REFUniStringReader::Read(OUT UCHAR* pBuffer, int iOffset, int nLength)
{
	BFX_REQUIRE (pBuffer != NULL);
	BFX_REQUIRE (iOffset >= 0 && nLength >= 0);

	int n = m_str.GetLength() - m_nPos;
	if (n > 0)
	{
		if (n > nLength) n = nLength;
		memcpy(pBuffer + iOffset, m_str.GetData() + m_nPos, n * sizeof(UCHAR));
		m_nPos += n;
	}
	return n;
}

}	//	namespace BFX
